
package com.chuangan.releasetask.Utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zihua
 * @Date: 2020/8/17 14:45
 * @Version 1.0
 */
public class FileUtils {

    //一张图片
    public static String uploadImg(MultipartFile uploadFile, String prefixFile ,String prefixDir ,HttpServletRequest req) {
        //获取文件名
        String fileName = uploadFile.getOriginalFilename();
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名
        fileName = prefixFile + suffixName;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
        //添加日期目录
        String format = sd.format(new Date());
        //指定本地文件夹存储图片
        String filePath = "C:/Data/uploadFile/" + format + "/" + prefixDir;
        File file = new File(filePath, fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            //将图片保存到static文件夹里
            file.createNewFile();
            uploadFile.transferTo(new File(filePath + fileName));
            //return filePath + fileName;
            return "http://" + "121.37.218.71" + ":" + req.getServerPort() + "/" + req.getContextPath() + "/" + format + "/" + prefixDir + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }












}

